package com.example.petadopt.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.entity.Pet;
import com.google.android.material.imageview.ShapeableImageView;
import com.gzone.university.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 热门宠物适配器
 */
public class HotPetAdapter extends RecyclerView.Adapter<HotPetAdapter.ViewHolder> {

    private List<Pet> petList = new ArrayList<>();
    private OnItemClickListener listener;

    public void setPetList(List<Pet> petList) {
        this.petList = petList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pet_home, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pet pet = petList.get(position);
        holder.bind(pet, listener);
    }

    @Override
    public int getItemCount() {
        return petList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ShapeableImageView ivPetImage;
        private TextView tvPetName;
        private TextView tvPetInfo;
        private TextView tvPetAddress;
        private Button btnAdopt;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetImage = itemView.findViewById(R.id.iv_pet_image);
            tvPetName = itemView.findViewById(R.id.tv_pet_name);
            tvPetInfo = itemView.findViewById(R.id.tv_pet_info);
            tvPetAddress = itemView.findViewById(R.id.tv_pet_address);
            btnAdopt = itemView.findViewById(R.id.btn_adopt);
        }

        public void bind(final Pet pet, final OnItemClickListener listener) {
            // 加载宠物图片
            if (pet.getImage() != null && !pet.getImage().isEmpty()) {
                GlideUtils.load(ivPetImage, pet.getImage());
            }

            // 设置宠物名称
            tvPetName.setText(pet.getName());

            // 设置宠物信息
            StringBuilder infoBuilder = new StringBuilder();
            infoBuilder.append(pet.getBreed());

            if (pet.getAge() != null) {
                infoBuilder.append(" · ").append(pet.getAge()).append("岁");
            }

            // 检查健康状态，如果有"已绝育"则显示
            List<String> healthStatus = pet.getHealthStatus();
            if (healthStatus != null && !healthStatus.isEmpty()) {
                for (String status : healthStatus) {
                    if (status.contains("已绝育")) {
                        infoBuilder.append(" · 已绝育");
                        break;
                    }
                }
            }

            tvPetInfo.setText(infoBuilder.toString());

            // 设置宠物地址
            tvPetAddress.setText(pet.getAddress());
            if (pet.getIsAdopted()) {
                // 如果宠物已被领养，设置按钮不可点击
                btnAdopt.setEnabled(false);
                btnAdopt.setText("已被领养");
            } else {
                // 如果宠物未被领养，设置按钮可点击
                btnAdopt.setEnabled(true);
                btnAdopt.setText("申请领养");
            }
            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listener != null) {
                        listener.onItemClick(pet);
                    }
                }
            });

            // 申请领养按钮点击事件
            btnAdopt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onAdoptClick(v, pet);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Pet pet);

        void onAdoptClick(View v, Pet pet);
    }
} 